
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "cJSON.h"

#define API_URL "http://api.openweathermap.org/data/2.5/weather?q=%s&appid=YOUR_API_KEY&units=metric&lang=zh_cn"

// 用于存储HTTP响应的缓冲区
struct MemoryStruct {
    char *memory;
    size_t size;
};

// libcurl的回调函数，用于写入数据
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    struct MemoryStruct *mem = (struct MemoryStruct *)userp;
    
    char *ptr = realloc(mem->memory, mem->size + realsize + 1);
    if(ptr == NULL) {
        printf("内存分配失败\n");
        return 0;
    }
    
    mem->memory = ptr;
    memcpy(&(mem->memory[mem->size]), contents, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
    
    return realsize;
}

// 解析JSON并提取天气信息
void parse_weather_json(const char *json_str) {
    cJSON *json = cJSON_Parse(json_str);
    if (json == NULL) {
        printf("JSON解析失败\n");
        return;
    }
    
    // 提取主要信息
    cJSON *main = cJSON_GetObjectItemCaseSensitive(json, "main");
    cJSON *weather = cJSON_GetObjectItemCaseSensitive(json, "weather");
    cJSON *name = cJSON_GetObjectItemCaseSensitive(json, "name");
    
    if (main && weather && name) {
        // 提取温度
        cJSON *temp = cJSON_GetObjectItemCaseSensitive(main, "temp");
        
        // 提取天气描述
        cJSON *weather_item = cJSON_GetArrayItem(weather, 0);
        cJSON *description = cJSON_GetObjectItemCaseSensitive(weather_item, "description");
        
        // 提取湿度
        cJSON *humidity = cJSON_GetObjectItemCaseSensitive(main, "humidity");
        
        printf("\n=== 天气信息 ===\n");
        printf("城市: %s\n", name->valuestring);
        printf("温度: %.1f°C\n", temp->valuedouble);
        printf("天气: %s\n", description->valuestring);
        printf("湿度: %d%%\n", humidity->valueint);
        printf("================\n");
    } else {
        cJSON *message = cJSON_GetObjectItemCaseSensitive(json, "message");
        if (message) {
            printf("错误: %s\n", message->valuestring);
        } else {
            printf("无法获取天气信息\n");
        }
    }
    
    cJSON_Delete(json);
}

int main() {
    char city[100];
    char url[512];
    
    printf("请输入城市名称: ");
    scanf("%99s", city);
    
    // 注意：这里需要替换为实际的API key
    // 为了演示，我们使用一个示例URL
    snprintf(url, sizeof(url), 
             "http://api.openweathermap.org/data/2.5/weather?q=%s&appid=b6907d289e10d714a6e88b30761fae22&units=metric&lang=zh_cn", 
             city);
    
    CURL *curl_handle;
    CURLcode res;
    
    struct MemoryStruct chunk;
    chunk.memory = malloc(1);
    chunk.size = 0;
    
    curl_global_init(CURL_GLOBAL_ALL);
    curl_handle = curl_easy_init();
    
    if(curl_handle) {
        curl_easy_setopt(curl_handle, CURLOPT_URL, url);
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
        curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
        
        res = curl_easy_perform(curl_handle);
        
        if(res != CURLE_OK) {
            printf("curl_easy_perform() 失败: %s\n", curl_easy_strerror(res));
        } else {
            parse_weather_json(chunk.memory);
        }
        
        curl_easy_cleanup(curl_handle);
    }
    
    curl_global_cleanup();
    
    if(chunk.memory) {
        free(chunk.memory);
    }
    
    return 0;
}
